package com.yic.module.qms.service.inspectscheme;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.qms.service.inspectschemeentry.InspectSchemeEntryService;
import com.yic.module.qms.service.inspectschemeitem.InspectSchemeItemService;
import com.yic.module.system.enums.dict.QmsSampleTypeEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.*;
import com.yic.module.qms.controller.admin.inspectscheme.vo.*;
import com.yic.module.qms.dal.dataobject.inspectscheme.InspectSchemeDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.qms.convert.inspectscheme.InspectSchemeConvert;
import com.yic.module.qms.dal.mysql.inspectscheme.InspectSchemeMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.qms.enums.ErrorCodeConstants.*;

/**
 * 检验方案 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectSchemeServiceImpl implements InspectSchemeService {

    @Resource
    private InspectSchemeMapper inspectSchemeMapper;
    @Resource
    @Lazy
    private InspectSchemeItemService inspectSchemeItemService;
    @Resource
    @Lazy
    private InspectSchemeEntryService inspectSchemeEntryService;

    @Override
    public Long createInspectScheme(InspectSchemeCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode(), createReqVO.getSampleType(), createReqVO.getProportion());
        // 插入
        InspectSchemeDO inspectScheme = InspectSchemeConvert.INSTANCE.convert(createReqVO);
        inspectSchemeMapper.insert(inspectScheme);
        // 返回
        return inspectScheme.getId();
    }

    @Override
    public void updateInspectScheme(InspectSchemeUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode(), updateReqVO.getSampleType(), updateReqVO.getProportion());
        // 更新
        InspectSchemeDO updateObj = InspectSchemeConvert.INSTANCE.convert(updateReqVO);
        inspectSchemeMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInspectScheme(Long id) {
        // 校验存在
        validateInspectSchemeExists(id);
        // 删除产品绑定
        inspectSchemeItemService.deleteInspectSchemeItemBySchemeId(id);
        // 删除检验项目绑定
        inspectSchemeEntryService.deleteInspectSchemeEntryBySchemeId(id);
        // 删除
        inspectSchemeMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String code, Integer sampleType, BigDecimal proportion) {
        validateCodeExists(id, code);
        validateSampletype(sampleType, proportion);
    }

    private void validateCodeExists(Long id, String code) {
        InspectSchemeDO inspectSchemeDO = inspectSchemeMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(inspectSchemeDO)) {
            return;
        }
        if (ObjectUtil.isEmpty(id)) {
            throw exception(INSPECT_SCHEME_CODE_EXISTS);
        }
        if (!id.equals(inspectSchemeDO.getId())) {
            throw exception(INSPECT_SCHEME_CODE_EXISTS);
        }
    }

    private void validateSampletype(Integer sampleType, BigDecimal proportion) {
        if (!QmsSampleTypeEnum.SAMPLE.getQmsSampleType().equals(sampleType)) {
            return;
        }
        if (QmsSampleTypeEnum.SAMPLE.getQmsSampleType().equals(sampleType) && ObjectUtil.isEmpty(proportion)) {
            throw exception(INSPECT_SCHEME_SAMPLE_NOT_EMPTY);
        }
        if (proportion.compareTo(BigDecimal.ZERO) < 0 || proportion.compareTo(new BigDecimal("100")) > 0) {
            throw exception(INSPECT_SCHEME_SAMPLE_NOT_RANGE);
        }
    }

    private void validateInspectSchemeExists(Long id) {
        if (inspectSchemeMapper.selectById(id) == null) {
            throw exception(INSPECT_SCHEME_NOT_EXISTS);
        }
    }

    @Override
    public InspectSchemeDO getInspectScheme(Long id) {
        return inspectSchemeMapper.selectById(id);
    }

    @Override
    public List<InspectSchemeDO> getInspectSchemeList(Collection<Long> ids) {
        return inspectSchemeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectSchemeDO> getInspectSchemePage(InspectSchemePageReqVO pageReqVO) {
        return inspectSchemeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectSchemeDO> getInspectSchemeList(InspectSchemeExportReqVO exportReqVO) {
        return inspectSchemeMapper.selectList(exportReqVO);
    }

}
